package com.sskj.community.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.community.R;
import com.sskj.community.R2;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.ArticleBean;
import com.zzhoujay.richtext.RichText;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/07/08
 */
@Route(path = RConfig.COMMUNITY_ACTIVITY_PUBLISH_ARTICLE)
public class PublishArticleActivity extends BaseActivity<PublishArticlePresenter> {


    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvContentItem)
    TextView tvContentItem;
    @BindView(R2.id.tvTime)
    TextView tvTime;
    @Autowired
    public String id;
    @BindView(R2.id.tvArticle)
    ImageView tvArticle;

    @Override
    public int getLayoutId() {
        return R.layout.community_activity_publish_article;
    }

    @Override
    public PublishArticlePresenter getPresenter() {
        return new PublishArticlePresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        mPresenter.getArticleData(id);
    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, PublishArticleActivity.class);
        context.startActivity(intent);
    }

    public void getArticleSuccess(ArticleBean content) {
        tvTitle.setText(content.getTitle());
        ImageUtil.setImage(BaseHttpConfig.BASE_URL + content.getImg(), tvArticle);
//        Glide.with(this).load(BaseHttpConfig.BASE_URL + content.getImg()).into(tvArticle);
        tvTime.setText(TimeFormatUtil.formatE(Long.valueOf(content.getCreate_time()) * 1000));
        RichText.fromHtml(formatImg(content.getContent())).into(tvContentItem);
    }


    private String formatImg(String law_agree) {
        Document document = Jsoup.parse(law_agree);
        Elements imgs = document.getElementsByTag("img");
        for (Element img : imgs) {
            img.attr("width", "100%").attr("height", "auto");
            String url = img.attr("src");
            if (!url.contains("http")) {
                url = BaseHttpConfig.BASE_URL + url;
            }
            img.attr("src", url);
        }
        return document.toString();
    }
}
